package com.youxin.cpcenter.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.bean.sms.RequestSmsBean;
import com.youxin.busfoundation.utils.security.AesUtils;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.service.SmsService;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;

/**
 * 短信接口,企业试用短信认证
 * @author yingde.cao
 *
 */
@RestController
public class SmsController extends BaseController{
	@Autowired
	private SmsService smsService;
	/**
	 * 短信联系企业联系人
	 * @param request
	 * @param httpServletResponse
	 * @param requestBean
	 * @return
	 */
	@LogMethod(id="ent.sms.contact",eventContent={})
	@RequestMapping(value = "/api/sms", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject sendSms(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestSmsBean requestBean) {
		try {			
			ObjectUtils.requestNonNull(requestBean,"参数不能为空,格式为JSON.");
			ObjectUtils.requestNonEmpty(requestBean.getMessage(), "短信信息不能为空");
			
			return smsService.sendMessage(requestBean);
		} catch (Exception e) {		
			logger.error("/api/sms", e);
			return ResponseObject.systemError(e);
		}
	}
	
	/**
	 * 试用版获取手机验证码
	 * @param request HttpServletRequest
	 * @param httpServletResponse HttpServletResponse
	 * @param requestBean RequestSmsBean
	 * @return ResponseObject
	 */
	@LogMethod(id="ent.sms.register.code",eventContent={})
	@RequestMapping(value = "/api/sms/register/code", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject registerMessage(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestLoginVerifyCode requestBean) {
		ObjectUtils.requestNonNull(requestBean,"参数不能为空,格式为JSON.");
		try {			
			if(!RegexUtils.isMobile(requestBean.getMobile()))
			{
				return ResponseObject.userFailure(BusinessMessages.NOT_VALID_MOBILE);
			}
			return smsService.registerMessage(requestBean);
		} catch (Exception e) {		
			logger.error("/api/sms/register/code", e);
			return ResponseObject.systemError(e);
		}
	}
}
